package com.example.wordmemory;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.widget.Toast;

import androidx.core.app.NotificationCompat;

import com.example.wordmemory.utils.WordDBHelper;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class ReviewReminderReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        // 只在定时提醒或系统时间变化时触发
        if ("com.example.wordmemory.TEST_REVIEW_REMINDER".equals(action)||
                Intent.ACTION_TIME_TICK.equals(action) ||
            Intent.ACTION_DATE_CHANGED.equals(action) ||
            Intent.ACTION_TIME_CHANGED.equals(action) ||
            context.getPackageName().equals(intent.getPackage())) {
            // 获取今日应复习的单词数
            WordDBHelper dbHelper = new WordDBHelper(context);
            String today = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());
            int reviewCount = dbHelper.countTodayNeedReviewWords();
            dbHelper.close();

            if (reviewCount > 0) {
                // 创建通知渠道（Android 8.0+）
                String channelId = "review_channel";
                String channelName = "复习提醒";
                NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
                    manager.createNotificationChannel(channel);
                }

                // 点击通知跳转到复习页面
                Intent reviewIntent = new Intent(context, WordLearning.class);
                reviewIntent.putExtra("mode", "review");
                PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, reviewIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);

                NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId)
                        .setSmallIcon(R.drawable.ic_review)
                        .setContentTitle("复习提醒")
                        .setContentText("你有 " + reviewCount + " 个单词需要复习，快来巩固记忆吧！")
                        .setContentIntent(pendingIntent)
                        .setAutoCancel(true)
                        .setPriority(NotificationCompat.PRIORITY_HIGH);

                manager.notify(1001, builder.build());
                Toast.makeText(context, "收到广播，reviewCount=" + reviewCount, Toast.LENGTH_SHORT).show();
            }

        }
    }

    // 查询今天需要复习的单词数量
    private int getTodayReviewCount(WordDBHelper dbHelper, String today) {
        int count = 0;
        try {
            android.database.Cursor c = dbHelper.getCalendarByDate(today);
            if (c.moveToFirst()) {
                count = c.getInt(c.getColumnIndexOrThrow("review_count"));
            }
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }
} 